<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: 28064
  Date: 2022/3/23
  Time: 15:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Test context listener</title>
</head>
<body>
<table border="1">
    <tr>
        <th scope="col">bookId</th>
        <th scope="col">title</th>
        <th scope="col">author</th>
        <th scope="col">publisher</th>
        <th scope="col">price</th>
    </tr>
    <%
        Connection connection = null;
        ServletContext context = null;
        context = config.getServletContext();
        int count = 0;
        context.setAttribute("count", count);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test";
            connection = DriverManager.getConnection(url, "Lingqin", "123456");
            Statement stmt = connection.createStatement();
            String sql = "select * from books";

            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                count++;
                context.setAttribute("count", count);
    %>
    <tr>
        <td><%=rs.getString(1)  %>
        </td>
        <td><%=rs.getString(2)  %>
        </td>
        <td><%=rs.getString(3)  %>
        </td>
        <td><%=rs.getString(4)  %>
        </td>
        <td><%=rs.getString(5)  %>
        </td>
    </tr>
    <%
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        context.removeAttribute("count");
    %>
</table>
</body>
</html>